rm(list = setdiff(ls(), lsf.str())) #remove all unnecessary files from workspace except functions

#load data 15
data15<- read.dta13("Study 1/Original Data/shp15_p_user.dta")
data15 <- data15[,which(colnames(data15)%in%c("idint", "p15p19", "p15c140", "p15c141", "p15c142", "p15c143", "p15c144", "p15c145", "p15c146", "p15c147", "p15c148", "p15c149", "p15c150", "p15c151", "p15c152", "p15c153", "p15c154", "sex15", "age15", "isced15", "i15ptotn", "plingu15", "p15p10", "p15r24"))]

#Vote for populist party
data15$vote_svp15 <- ifelse(as.numeric(data15$p15p19)==9,1, ifelse(as.numeric(data15$p15p19)==3,NA,0))

#SVP, other, not
data15$vote_svp_other_not<-ifelse(as.numeric(data15$p15p19)==9,1,
                                ifelse(as.numeric(data15$p15p19)==3,NA, 
                                       ifelse(as.numeric(data15$p15p19)==26 | as.numeric(data15$p15p19)==27,3,2)))
data15$vote_svp_other_not<-as.factor(data15$vote_svp_other_not)
levels(data15$vote_svp_other_not) <- c("Populist","Other parties","Not")
data15$vote_svp_other_not <- relevel(data15$vote_svp_other_not, ref = "Populist")


#Conscientiousness 
#thorough
data15$c1_thorough<-car::recode(data15$p15c140,"-3=NA; -2=NA; -1=NA")
data15$c2_lazy<-car::recode(data15$p15c146,"-3=NA; -2=NA; -1=NA")
data15$c2_lazy<-10-data15$c2_lazy
data15$c3_efficient<-car::recode(data15$p15c150,"-3=NA; -2=NA; -1=NA")
data15$con <- rowMeans(with(data15,data.frame(c2_lazy,c1_thorough, c3_efficient)),na.rm=T)

#Extraversion 
data15$e1_talkative<-car::recode(data15$p15c141,"-3=NA; -2=NA; -1=NA")
data15$e2_sociable<-car::recode(data15$p15c147,"-3=NA; -2=NA; -1=NA")
data15$e3_reserved<-car::recode(data15$p15c151,"-3=NA; -2=NA; -1=NA")
data15$e3_reserved_rec<-10-data15$e3_reserved
data15$ext <- rowMeans(with(data15,data.frame(e1_talkative,e2_sociable, e3_reserved_rec)),na.rm=T)

#Agreeableness 
#rude
data15$a1_rude_rec<-10-car::recode(data15$p15c142,"-3=NA; -2=NA; -1=NA")
data15$a2_forgive<-car::recode(data15$p15c145,"-3=NA; -2=NA; -1=NA")
data15$a3_kind<-car::recode(data15$p15c152,"-3=NA; -2=NA; -1=NA")
data15$agre <- rowMeans(with(data15,data.frame(a1_rude_rec,a2_forgive, a3_kind)),na.rm=T)

#Openness 
#Original
data15$o1_original<-car::recode(data15$p15c143,"-3=NA; -2=NA; -1=NA")
data15$o2_artistic<-car::recode(data15$p15c148,"-3=NA; -2=NA; -1=NA")
data15$o3_imagination<-car::recode(data15$p15c153,"-3=NA; -2=NA; -1=NA")
data15$open <- rowMeans(with(data15,data.frame(o1_original,o2_artistic, o3_imagination)),na.rm=T)

#Neuroticism 
#Worry
data15$n1_worry<-car::recode(data15$p15c144,"-3=NA; -2=NA; -1=NA")
data15$n2_nervous<-car::recode(data15$p15c149,"-3=NA; -2=NA; -1=NA")
data15$n3_relaxed<-car::recode(data15$p15c154,"-3=NA; -2=NA; -1=NA")
data15$n3_relaxed_rec<-10-data15$n3_relaxed
data15$neu <- rowMeans(with(data15,data.frame(n1_worry,n2_nervous, n3_relaxed_rec)),na.rm=T)

#female
data15$female<-car::recode(as.numeric(data15$sex15),"6=0; 7=1")

#age
data15$age<-data15$age15
data15$age<-car::recode(data15$age,"-2=NA; -1=NA; 0=NA; 1=NA; 2=NA; 3=NA; 4=NA; 5=NA; 6=NA; 7=NA; 8=NA; 9=NA; 10=NA; 11=NA; 12=NA; 13=NA; 14=NA; 15=NA; 16=NA; 17=NA")

#education
data15$education<-as.numeric(data15$isced15)
data15$education<-car::recode(data15$education,"1=NA; 2=NA; 3=NA")
data15$education<-data15$education-3

#income
data15$income<-car::recode(data15$i15ptotn, "-8=NA; -4=NA; -3=NA; -2=NA; -1=NA")
data15$income_cat<-ifelse(data15$income<=2000, 1,
                        ifelse(data15$income> 2000 & data15$income<10000, 2,
                               ifelse(data15$income> 10000 & data15$income<20000, 3,
                                      ifelse(data15$income> 20000 & data15$income<30000, 4,
                                             ifelse(data15$income> 30000 & data15$income<40000, 5,
                                                    ifelse(data15$income> 40000 & data15$income<50000, 6,
                                                           ifelse(data15$income> 50000 & data15$income<60000, 7,
                                                                  ifelse(data15$income> 60000 & data15$income<70000, 8,
                                                                         ifelse(data15$income> 70000 & data15$income<80000, 9,
                                                                                ifelse(data15$income> 80000 & data15$income<90000,10, 
                                                                                       ifelse(data15$income> 90000 & data15$income<100000,11,
                                                                                              ifelse(data15$income> 100000 & data15$income<150000,12, 
                                                                                                     ifelse(data15$income> 150000,13, NA)))))))))))))

data15$income<-zero1(data15$income_cat)
data15$income[is.na(data15$income)==TRUE]=2

#income missing
data15$income_mis<-ifelse(data15$income==2,1,0)

#language
data15$language[as.numeric(data15$plingu15)==6]=1
data15$language[as.numeric(data15$plingu15)==7]=2
data15$language[as.numeric(data15$plingu15)==8]=3

#left-right
data15$lr_placement<-data15$p15p10
data15$lr_placement<-car::recode(data15$lr_placement,"-5=NA; -4=NA; -3=NA; -2=NA; -1=NA")

#anti-islam
data15$anti_islam<-data15$p15r24
data15$anti_islam<-car::recode(data15$anti_islam,"-5=NA; -4=NA; -3=NA; -2=NA; -1=NA")
data15$anti_islam<-(10-data15$anti_islam)

data15<-data15[complete.cases(data15$vote_svp15), ]
data15<-data15[complete.cases(data15$agre), ]

data15$language1<-ifelse(data15$language==1,1,0)
data15$language2<-ifelse(data15$language==2,1,0)
data15$language3<-ifelse(data15$language==3,1,0)

data15$edu1<-ifelse(data15$education==2,1,0)
data15$edu2<-ifelse(data15$education==3,1,0)
data15$edu3<-ifelse(data15$education==4,1,0)
data15$edu4<-ifelse(data15$education==5,1,0)
data15$edu5<-ifelse(data15$education==6,1,0)
data15$edu6<-ifelse(data15$education==7,1,0)
data15$edu7<-ifelse(data15$education==8,1,0)
data15$edu8<-ifelse(data15$education==9,1,0)
data15$edu9<-ifelse(data15$education==10,1,0)

save(data15, file="Study 1/Altered Data/Study1_Swiss_Household15.RData")


